/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nokia.iap.ui.lib.onebutton;

import com.nokia.mid.ui.CanvasGraphicsItem;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author zinin
 */
public class GxMasterBar extends CanvasGraphicsItem implements GxControl, GxButtonDrawer.Listener{
    private final GxColorTheme theme;
    private final int margin = 5;
    private final int progressMax;
    private int progressCurr;
    private final int height;
    private final int width;
    private final int panecolor;
    private final int x;
    private final int y;
    private GxButtonBase closeBtn = null;
    private OneButtonIAP client = null;

    public GxMasterBar(OneButtonIAP aClient, GxColorTheme colour, int progressMax){
        super(10, 10);
        client = aClient;
        theme = colour;
        panecolor = GxColorTheme.lighten(theme.getBackgroundColor(), 0.35);
        
        height = theme.getFont().getHeight() + margin * 2;
        this.width = client.getWidth();
        setSize(width, height);
        x = 0;
        y = client.getHeight() - height;
        
        this.progressMax = progressMax;
        progressCurr = 0;
        
        closeBtn = new GxButtonCloseIAP(client, "X", client.getWidth() - height, 0, theme);
        closeBtn.drawer.addListener(this);
    }

    public void advance(){
        progressCurr++;
        if(progressCurr > progressMax){
            progressCurr = progressMax;
        }
    }
    
    public void setToBegin(){
        progressCurr = 0;
    }

    protected void paint(Graphics g) {
        int oldColor = g.getColor();
        g.setColor(panecolor);

        g.translate(x, y);
        g.fillRect(0, 0, width, height);

        int diameter = (int)(height * 0.60);
        int curcleY = (height - diameter) / 2;
        int margin = diameter / 2;
        int currX = margin;
        int selectedColor = 0xDB610F;
        int nonSelectedColor = 0xA4BDD9;
        for(int i = 0; i < progressMax; i++){
            g.setColor((i <= progressCurr) ? selectedColor : nonSelectedColor);
            g.fillArc(currX, curcleY, diameter, diameter, 0, 360);
            currX += diameter + margin;
        }
        closeBtn.paint(g);
        
        g.translate(-x, -y);
        g.setColor(oldColor);
    }

    public void clicked(GxButtonDrawer button) {
        if(button.isPressed()){
            client.exitIAP();
        }
    }

    public void setFocus(boolean isFocused) {
        closeBtn.setFocus(isFocused);
    }

    public void execute() {
        closeBtn.execute();
    }

    public boolean onTouch(int pointerX, int pointerY) {
        return (closeBtn.onTouch(pointerX - x, pointerY - y));
    }

    public boolean isHidden() {
        return false;
    }
}
